package com.joker.aigc.oj.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.joker.aigc.domain.entity.R;
import com.joker.aigc.oj.domain.entity.Recruit;
import com.joker.aigc.oj.domain.entity.RecruitApply;
import com.joker.aigc.oj.service.RecruitApplyService;
import com.joker.aigc.oj.service.RecruitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Validated
@Slf4j
@RestController
@RequestMapping("/recruit")
@Tag(name = "RecruitController",description = "招聘信息查询接口")
public class RecruitController {


    @Autowired
    RecruitService recruitService;

    @Autowired
    RecruitApplyService recruitApplicationService;


    @Operation(summary = "根据用户，来获取用户信息")
    @RequestMapping(value = "/selectAllByUser", method = RequestMethod.POST)
    public R<IPage<RecruitApply>> selectAllByUser(@NotNull(message = "页数不能为空") @RequestParam("current") Integer current) {
        IPage<RecruitApply> recruitApplicationIPage = recruitApplicationService.selectAllRecruitApplicationByUser(current);
        if (!recruitApplicationIPage.getRecords().isEmpty()) {
            return R.ok(recruitApplicationIPage);
        } else {
            return R.error(201,"对不起现在没有任何招聘要求");
        }
    }


    @Operation(summary = "step 1: 管理员查看简历，筛选用户")
    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    public R<IPage<Recruit>> selectAll(@NotNull(message = "页数不能为空") @RequestParam("current") Integer current) {
        IPage<Recruit> result = recruitService.selectAllRecruit(current);
        if (!result.getRecords().isEmpty()) {
            return R.ok(result);
        } else {
            return R.error(201,"查询失败");
        }
    }


    @Operation(summary = "添加求职信息")
    @RequestMapping(value = "/addRecruit", method = RequestMethod.POST)
    public R<String> addRecruit(@Valid @RequestBody Recruit recruit) {
        Integer result = recruitService.addRecruit(recruit);
        if (result > 0) {
            return R.ok("添加成功");
        } else if (result == -2) {
            return R.error(201,"没有该岗位。raid 填写错误");
        } else {
            return R.error(201,"添加失败");
        }
    }


    @Operation(summary = "step 2: 管理员查看简历，筛选用户，给笔试或面试机会")
    @RequestMapping(value = "/pass", method = RequestMethod.POST)
    public R<String> pass(@NotEmpty @RequestBody List<Long> ids) {

        Integer result = recruitService.pass(ids);

        if (result > 0) {
            return R.ok("添加成功");
        } else {
            return R.error( 201,"添加失败");
        }
    }

}
